<?php

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Appbundle\Entity\BlogPost;
class BlogPostAdmin extends AbstractAdmin{
  protected function configureFormFields(FormMapper $formMapper){
    $formMapper
              ->with('content')
                ->add('title','text')
                ->add('body', 'textarea')
              ->end()

              ->with('Meta data')
                ->add('category','entity',array(
                  'class' => 'AppBundle\Entity\Category',
                  'choice_label' => 'name'
                ))
              ->end()
            ;
  }

  protected function configureDatagridFilters(DatagridMapper $datagridMapper){
    $datagridMapper
        ->add('title')
        ->add('category',null,array(),'entity',array(
          'class'=>'AppBundle\Entity\Category',
          'choice_label' => 'name',
        ));
  }

  protected function configureListFields(ListMapper $listMapper){

    $listMapper
        ->addIdentifier('title')
        ->add('category.name')
        ->add('draft');
  }

  public function toString($object){
    return $object instanceof BlogPost
        ? $object->getTitle()
        : 'Blog Post';
  }
}
